package com.scm.exception;

/**
 * @author CHL
 * @ClassName: ControllerExceptionHandler
 * @Description: TODO
 * @date 2022/3/8 15:12
 * @Version 1.0
 */
import com.scm.entity.response.ResponseMessage;
import java.io.PrintStream;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ControllerExceptionHandler
{
    @ExceptionHandler({Exception.class})
    @ResponseBody
    public ResponseMessage handlerException(Exception ex)
    {
        System.out.println(ex + "����****");
        ex.printStackTrace();
        ResponseMessage result = new ResponseMessage();

        result.setCode(Integer.valueOf(4));
        if ((ex instanceof DataIntegrityViolationException))
        {
            System.out.println("---");
            result.setMessage("������������������������ �� " + ex.getMessage());
        }
        else if ((ex instanceof DuplicateKeyException))
        {
            result.setMessage("������������������");
        }
        else if ((ex instanceof TransactionException))
        {
            result.setMessage(ex.getMessage());
        }
        else if ((ex instanceof NumberFormatException))
        {
            result.setMessage("������������" + ex.getMessage());
        }
        else
        {
            result.setMessage(ex.getMessage());
        }
        return result;
    }
}

